//
//  MoneyQureyViewController.swift
//  WangPu
//
//  Created by xiaozhi on 15/10/24.
//  Copyright © 2015年 zhixiao. All rights reserved.
//

import UIKit

class MoneyQureyViewController: BaseViewController {
    var moeny:String?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "账户余额"
        let backItem = UIBarButtonItem(image: UIImage(named: "back_icon"), style:UIBarButtonItemStyle.Plain, target:self, action:"goBack")
        backItem.tintColor = UIColor.whiteColor()
        self.navigationItem.leftBarButtonItem = backItem
        self.automaticallyAdjustsScrollViewInsets = false
        view.backgroundColor = commonBackgoundColor
        // Do any additional setup after loading the view.
        
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBarHidden = false
        if let sessionid = GlobalDataHelper.instance.session{
            self.queryPoint(sessionid)
        }
    }
    func setupViews()->Void{
        let baseHieght:CGFloat = 40.0
        let labelX:CGFloat = 32.0
        let offsetX:CGFloat = 10.0
        let line1 = UIView(frame: CGRectMake(0, 74, view.bounds.size.width, baseHieght))
        line1.backgroundColor = UIColor.whiteColor()
        let userImgView = UIImageView(frame: CGRectMake(offsetX, (line1.bounds.size.height-16)/2.0, 16, 16))
        userImgView.image = UIImage(named: "pay_icon")
        line1.addSubview(userImgView)
        let note1 = UILabel(frame: CGRectMake(labelX, 0, 75, baseHieght))
        note1.text = "可用余额"
        note1.font = UIFont.systemFontOfSize(13)
        line1.addSubview(note1)
        
        let point = UILabel(frame: CGRectMake(view.bounds.size.width-110, 0, 100, baseHieght))
        point.text = moeny!+"元"
        point.textColor = commonRedColor
        point.textAlignment = NSTextAlignment.Right
        point.font = UIFont.systemFontOfSize(13)
        line1.addSubview(point)
        view.addSubview(line1)
    }
    func queryPoint(sessionid:String!)->Void{
        let request = NetWorkHelper()
        let parameters = NSMutableDictionary()
        parameters.setObject(sessionid, forKey: "sessionid")
        request.requestStart("/user/getBalance", parameters: parameters,compeleteHandle:{ (data, response, error)  in
            dispatch_async(dispatch_get_main_queue()){
                self.hiddenProcessView()
            }
            if error == nil{
                var dic:NSDictionary?
                do {
                    dic = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments) as? NSDictionary
                } catch let error as NSError {
                    print(error)
                    dispatch_async(dispatch_get_main_queue()){
                        self.showTips("数据加载出错")
                    }
                    return
                }
                if (dic?["result"])! as! String == "0"{
                    dispatch_async(dispatch_get_main_queue()){
                        self.moeny = dic?["data"] as? String
                        self.setupViews()
                    }
                }
                dispatch_async(dispatch_get_main_queue()){
                }
            }
            else{
                self.showTips("加载失败")
            }
        })
        self.showProcess("加载中...")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func  goBack(){
        self.navigationController?.popViewControllerAnimated(true)
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
